#include <qapplication.h>
#include <qsplashscreen.h>
#include <xinifile.h>
#include <assert.h>
#include <xwritelogexport.h>
#include <QWSServer>
#include <qpalette.h>
#include "ui_sm700_main.h"
#include "sm700.h"

int main(int argc, char ** argv) 
{
	QApplication a(argc, argv);
	QWSServer::setCursorVisible(false);
	QWSServer::setDesktopBackground(QColor(Qt::white));
	XIniFile iniFile;
	QString txt;
	QPixmap pixmap("/usr/infomat/splash.png");
	QSplashScreen splash(pixmap, Qt::WindowStaysOnTopHint);
	splash.setCursor(Qt::BlankCursor);
	/* ******** Software Version ************ */
	int major, minor, release, build; 
	QString swVersionTxt;
	assert(0 == iniFile.open("./VERSION"));
	if(0!=iniFile.readInt("","MAJOR", major))
		LOG_E("Major Error");
	swVersionTxt = swVersionTxt.setNum(major) + QString(".");

	if(0!=iniFile.readInt("","MINOR", minor))
		LOG_E("Minor Error");
	QString tmp ;
	tmp.setNum(minor);
	swVersionTxt =  swVersionTxt.append(tmp) + QString(".");

	if(0!=iniFile.readInt("","RELEASE", release))
	  LOG_E("Release Error");
	tmp.setNum(release);
	swVersionTxt =  swVersionTxt.append(tmp) + QString("-");

	if(0!=iniFile.readInt("","BUILD", build))
	  LOG_E("Build Error");
	tmp.setNum(build);
	swVersionTxt =  swVersionTxt.append(tmp);
	iniFile.close();

	/* ******* Qt version ******************* */
	char QtVersion[32] = {0} ;
	if (0!=iniFile.open("/etc/Qt.conf")) {
		LOG_E("read Qt.conf error");
	}
	if (0!=iniFile.readString("","Ver",QtVersion,32)) {
		LOG_E("read qt version error");
	}

	iniFile.close();
	/* ******* OS version ******************* */
	FILE *fpin;
	char line[256] = {0};
	if (NULL == (fpin=popen("uname -svr", "r")))
	  LOG_E("popen error");

	for (;;) {
		if (fgets(line, 256, fpin) == NULL) /* read from pipe */
		  break;
		if (fputs(line, stdout) == EOF)
		  LOG_E("fputs error to pipe");
	}
	if (pclose(fpin) == -1)
	  LOG_E("pclose error");

	QString tmpOs = QString(line);
	QString osVer = tmpOs.section(' ',0,2);
	
	/*************** IP ****************** */
	assert(0 == iniFile.open("/usr/infomat/udpd.conf"));
	char address[64];
	if (0!=iniFile.readString("","IPV4ADDRESS",address,64))
		LOG_E("splash: Read Ip Failed");
	iniFile.close();
	QString ipTxt;
	QString ipValue = QString(address);
	ipTxt = ipTxt.append(ipValue);
	a.processEvents();
	QLabel * lbSoftware = new QLabel(&splash);
	QLabel * lbOsVersion = new QLabel(&splash);
	QLabel * lbIp  = new QLabel(&splash);
	QLabel * lbQtVersion = new QLabel(&splash);
	QFont font;  
	font.setPointSize(75);
	font.setWeight(QFont::Normal);
	font.setFamily(QString("SF Willamette"));
	
	lbSoftware->setFont(font);
	lbSoftware->setPaletteForegroundColor(Qt::white);
	lbSoftware->setText(QString("Software: ") + swVersionTxt);
//	lbSoftware->setText(QString("Preview: ") + QString("0.10.3.13"));
	lbSoftware->setGeometry(QRect(100,100,600,200));
	
	lbOsVersion->setFont(font);
	lbOsVersion->setPaletteForegroundColor(Qt::white);
	lbOsVersion->setText(QString("OS: ")+osVer);
	lbOsVersion->setGeometry(QRect(100,200,600,150));
	
	lbIp->setFont(font);
	lbIp->setPaletteForegroundColor(Qt::white);
	lbIp->setText(QString("IP: ")+ipTxt);
	lbIp->setGeometry(QRect(100, 300, 600,150));
	
	lbQtVersion->setFont(font);
	lbQtVersion->setPaletteForegroundColor(Qt::white);
	lbQtVersion->setText(QString("Qt: ") + QString(QtVersion));
	lbQtVersion->setGeometry(QRect(100, 400, 600,150));
	splash.show();

	/***********End ShowSplash*******************/
	MainWidget w (0, Qt::FramelessWindowHint);
	splash.finish(&w);
	a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
	a.setOverrideCursor(QCursor(Qt::BlankCursor));
	return a.exec();
}
